﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using MySql.Data.MySqlClient;

/// <summary>
/// Summary description for StageService
/// </summary>
[WebService(Namespace = "http://main.hxhieu.net/Services/StageService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class StageService : System.Web.Services.WebService {

    private ConnectionInfo _local = new ConnectionInfo("localhost", "ocean_dev", "devdev", "hxhieu_ocean");
    private ConnectionInfo _remote = new ConnectionInfo("98.130.0.78", "hxhieu_ocean", "Dev111", "hxhieu_ocean");
    
    public StageService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    private MySqlConnection newConnection()
    {
        ConnectionInfo conInfo = _remote;

        string connectionString = "Database=" + conInfo.Database +
            ";Data Source=" + conInfo.HostAddress +
            ";User Id=" + conInfo.UserName +
            ";Password=" + conInfo.PassWord;

        MySqlConnection connection = new MySqlConnection(connectionString);

        return connection;
    }

    /// <summary>
    /// Get the stage start up information
    /// </summary>
    /// <returns>The string contains bgcolor, music, sound, label status</returns>
    [WebMethod]
    public string getStageInfo() {

        string info = "";
        MySqlConnection connection = newConnection();

        try
        {
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "SELECT * FROM ocean_stage_stg";
            MySqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                info += reader.GetString("stg_bgcolor")+ ",";
                info += reader.GetString("stg_music") + ",";
                info += reader.GetString("stg_sound") + ",";
                info += reader.GetString("stg_label") + ",";
            }
            reader.Close();
            reader = null;
        }
        catch (MySqlException ex)
        {
            Console.WriteLine(ex.Number.ToString());
        }
        finally
        {
            
            connection.Close();
            connection = null;
        }
        return info;
    }

    /// <summary>
    /// Update the bgcolor to the DB
    /// </summary>
    /// <param name="color">The uint color value</param>
    /// <returns>Succeeded/Failed</returns>
    [WebMethod]
    public bool setStageColor(UInt32 color)
    {
        bool result;
        MySqlConnection connection = newConnection();

        try
        {
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "UPDATE ocean_stage_stg SET stg_bgcolor = @bgcolor";
            command.Prepare();
            command.Parameters.Add("@bgcolor", MySqlDbType.UInt32).Value = color;
            command.ExecuteNonQuery();
            result = true;
        }
        catch (MySqlException ex)
        {
            result = false;
            Console.WriteLine(ex.Number.ToString());
        }
        finally
        {

            connection.Close();
            connection = null;
        }
        return result;
    }

    /// <summary>
    /// Save the music state on/off to the DB
    /// </summary>
    /// <param name="val">On/off</param>
    /// <returns>Succeeded/Failed</returns>
    [WebMethod]
    public bool setStageMusic(bool val)
    {
        bool result;
        MySqlConnection connection = newConnection();

        try
        {
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "UPDATE ocean_stage_stg SET stg_music = @music";
            command.Prepare();
            command.Parameters.Add("@music", MySqlDbType.Bit).Value = val;
            command.ExecuteNonQuery();
            result = true;
        }
        catch (MySqlException ex)
        {
            result = false;
            Console.WriteLine(ex.Number.ToString());
        }
        finally
        {

            connection.Close();
            connection = null;
        }
        return result;
    }

    /// <summary>
    /// Save the sound state on/off to the DB
    /// </summary>
    /// <param name="val">On/off</param>
    /// <returns>Succeeded/Failed</returns>
    [WebMethod]
    public bool setStageSound(bool val)
    {
        bool result;
        MySqlConnection connection = newConnection();

        try
        {
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "UPDATE ocean_stage_stg SET stg_sound = @sound";
            command.Prepare();
            command.Parameters.Add("@sound", MySqlDbType.Bit).Value = val;
            command.ExecuteNonQuery();
            result = true;
        }
        catch (MySqlException ex)
        {
            result = false;
            Console.WriteLine(ex.Number.ToString());
        }
        finally
        {

            connection.Close();
            connection = null;
        }
        return result;
    }

    /// <summary>
    /// Save the label state on/off to the DB
    /// </summary>
    /// <param name="val">On/off</param>
    /// <returns>Succeeded/Failed</returns>
    [WebMethod]
    public bool setStageLabel(bool val)
    {
        bool result;
        MySqlConnection connection = newConnection();

        try
        {
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "UPDATE ocean_stage_stg SET stg_label = @label";
            command.Prepare();
            command.Parameters.Add("@label", MySqlDbType.Bit).Value = val;
            command.ExecuteNonQuery();
            result = true;
        }
        catch (MySqlException ex)
        {
            result = false;
            Console.WriteLine(ex.Number.ToString());
        }
        finally
        {

            connection.Close();
            connection = null;
        }
        return result;
    }
}

